--
-- Author: shunguo.chen
-- Date: 2019-05-24 14:25:47
--
-- RuneTipPanel 符文tips
--

local newFromBtnView = require("app.newFloatTips.floatSubmodule.newFromBtnView") -- 来源按钮

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RuneTipsNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._panelWidth, self._panelHeight  = self._ui.Panel:getContentSize2()
    self:setContentSize( cc.size( self._panelWidth, self._panelHeight ) )

	self._skillIcon = self._ui["Panel/skill_icon/icon"]
	self._skillNameText = self._ui["Panel/skill_name"]
	self._skillDescText = self._ui.Panel:getChildByName( "text" ) -- 技能描述文本
	self._skillDescTextWidth, self._skillDescTextHeight = self._skillDescText:getContentSize2()

	self._runeIcon = self._ui["Panel/rune_icon/icon"]
	self._runeDescScrollView = self._ui.Panel:getChildByName( "ScrollView_1" )
	self._runeDescTextWidth, self._runeDescTextHeight = self._runeDescScrollView:getContentSize2()
	self._runeDescText = self._runeDescScrollView:getChildByName( "text" ) -- 符文描述文本
	
	-- 来源按钮
	local fromPanel = self._ui.Panel_from
	fromPanel:setVisible(false)

	self._fromBtnTips = newFromBtnView.new()
	self._fromBtnTips:setPosition(fromPanel:getPosition())
	self._fromBtnTips:setAnchorPoint(fromPanel:getAnchorPoint())
	self._fromBtnTips:setClickCB( handler( self, self._clickCb ) )
	self._ui.Panel:addChild(self._fromBtnTips)
end

-- data = Rune
function ClassRef:refresh()
	if self._data then
		self:setVisible( true )

		local rune = self._data

		-- 符文
		self._runeIcon:loadTexture(rune:getIcon())

		self._runeDescText:getVirtualRenderer():setDimensions( self._runeDescTextWidth, 0 )
		self._runeDescText:setString( rune:getDesc() )
		local runeTextSize = self._runeDescText:getVirtualRendererSize()
		self._runeDescText:setContentSize( cc.size( self._runeDescTextWidth, runeTextSize.height ) )
		self._runeDescText:setPositionY( runeTextSize.height )
		self._runeDescScrollView:getInnerContainer():setContentSize( cc.size( self._runeDescTextWidth, runeTextSize.height ) )
		self._runeDescScrollView:getInnerContainer():setPositionY( self._runeDescTextHeight - runeTextSize.height )
		self._runeDescScrollView:setTouchEnabled( runeTextSize.height > self._runeDescTextHeight )

		-- 技能
		self._skillIcon:loadTexture(rune:getSkillIcon())
		self._skillNameText:setString(rune:getSkillName())

		self._skillDescText:getVirtualRenderer():setDimensions( self._skillDescTextWidth, 0 )
		self._skillDescText:setString(rune:getSkillDesc())
		local skillTextSize = self._skillDescText:getVirtualRendererSize()
		self._skillDescText:setContentSize( cc.size( self._skillDescTextWidth, skillTextSize.height ) )

		-- 调整panel高
		self._ui.Panel:setContentSize( cc.size( self._panelWidth, self._panelHeight + skillTextSize.height ) )
		self:setContentSize( cc.size( self._panelWidth, self._panelHeight + skillTextSize.height ) )

		self._ui.Panel:forceDoLayout()
		ccui.Helper:doLayout( self._ui.Panel )

		local _getResource = rune:getResource()
		if _getResource then -- 存在掉落来源？
			self._fromBtnTips:refreshContent( _getResource )
		end
		-- self:_refreshBtnContent()
	else
		self:setVisible( false )
	end
end

-- 设置回调函数
function ClassRef:registerCb( cb )
	self._clickCb = cb
end

-- 子node点击回调
function ClassRef:_clickCb( op, ... )
	executeFunc(self._clickCb,"closeRuneTips")
	local data = ...
	if "goto_feature" == op then
		display.resourceGotoFeature(data)
	end
end

return ClassRef
